package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.Result;
import com.itheima.pojo.Blog;
import com.itheima.pojo.Link;
import com.itheima.pojo.Tag;
import com.itheima.pojo.WebConfig;
import com.itheima.service.BlogService;
import com.itheima.service.LinkService;
import com.itheima.service.TagService;
import com.itheima.service.WebConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName IndexController
 * @Description 首页 前端控制器
 * @Author pengkaka
 * @Date 2020/8/7 9:11
 * @Version 1.0
 **/
@RestController
@RequestMapping("/index")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class IndexController {

    // 设置首页展示友链数量
    @Value(value = "${BLOG.FRIENDLY_LINK_COUNT}")
    private Integer FRIENDLY_LINK_COUNT;

    // 设置热门博客数量
    @Value(value = "${BLOG.HOT_COUNT}")
    private Integer BLOG_HOT_COUNT;

    // 设置最新博客数据
    @Value(value = "${BLOG.NEW_COUNT}")
    private Integer BLOG_NEW_COUNT;

    // 设置最热标签数量
    @Value(value = "${BLOG.HOT_TAG_COUNT}")
    private Integer BLOG_HOT_TAG_COUNT;

    @Autowired
    private LinkService linkService;

    @Autowired
    private WebConfigService webConfigService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private TagService tagService;


    // 获取友情链接
    @GetMapping("/getLink")
    public Result getLink() {
        List<Link> linkList = linkService.getListByPageSize(FRIENDLY_LINK_COUNT);
        return new Result("success", linkList);
    }

    // 增加友情链接点击数
    @GetMapping("/addLinkCount")
    public Result addLinkCount(@RequestParam("uid") String uid) {
        try {
            linkService.addLinkCount(uid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "更新失败");
        }
        return new Result("success", "更新成功");
    }

    // 获取网站配置
    @GetMapping("/getWebConfig")
    public Result getWebConfig() {
        WebConfig webConfig = webConfigService.getWebConfig().getData();
        return new Result("success", webConfig);
    }

    // 获取首页最新的博客
    @GetMapping("/getNewBlog")
    public Result getNewBlog(@RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                             @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        List<Blog> pageList = blogService.getNewBlog(currentPage, BLOG_NEW_COUNT);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setCurrent(currentPage);
        result.setTotal(pageList.size());
        result.setRecords(pageList);

        return new Result("success", result);
    }

    // 获取首页排行博客
    @GetMapping("/getHotBlog")
    public Result getHotBlog() {
        List<Blog> pageList = blogService.getHotBlog(BLOG_HOT_COUNT);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setCurrent(1);
        result.setTotal(pageList.size());
        result.setRecords(pageList);

        return new Result("success", result);
    }

    // 获取最热标签
    @GetMapping("/getHotTag")
    public Result getHotTag() {
        Page<Tag> pageList = tagService.getHotTag(BLOG_HOT_TAG_COUNT);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setCurrent(1);
        result.setTotal((int) pageList.getTotal());
        result.setRecords(pageList.getResult());

        return new Result("success", result);
    }

    // 通过推荐等级获取博客列表
    @GetMapping("/getBlogByLevel")
    public Result getBlogByLevel(@RequestParam(name = "level", required = false, defaultValue = "0") Integer level,
                                 @RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                 @RequestParam(name = "useSort", required = false, defaultValue = "0") Integer useSort) {
        Page<Blog> pageList = blogService.getBlogPageByLevel(level, currentPage, useSort);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setCurrent(currentPage);
        result.setTotal((int) pageList.getTotal());
        result.setRecords(pageList.getResult());

        return new Result("success", result);
    }

    // 按时间戳获取博客
    @GetMapping("/getBlogByTime")
    public Result getBlogByTime(@RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        List<Blog> pageList = blogService.getBlogByTime(currentPage, pageSize);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setCurrent(currentPage);
        result.setTotal(pageList.size());
        result.setRecords(pageList);

        return new Result("success", result);
    }


}